package com.example.controller;

import com.example.entity.authority.SystemAuthor;
import com.example.ext.entity.ExtSystemAuthor;
import com.example.service.SystemAuthorService;
import com.example.util.ResultUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by youkun on 2017/9/2.
 */
@RestController
@RequestMapping("/api")
public class SystemauthorController {

    @Autowired
    private SystemAuthorService systemAuthorService;

    @ApiOperation(value = "查询功能列表的信息",notes = "获取功能列表信息")
    @RequestMapping(value="/querySystemAuthorList",method = RequestMethod.POST)
    Map<String,Object> querySystemAuthorList(@RequestBody ExtSystemAuthor extSystemAuthor) {

        return systemAuthorService.querySystemAuthorList(extSystemAuthor);
    }


    @ApiOperation(value = "添加功能的信息",notes = "添加功能的信息")
    @RequestMapping(value="/addSystemAuthor",method = RequestMethod.POST)
    Map<String,Object> addSystemAuthor(@RequestBody SystemAuthor systemAuthor) {

        return systemAuthorService.addSystemAuthor(systemAuthor);
    }

    @ApiOperation(value = "修改功能的信息",notes = "修改功能的信息")
    @RequestMapping(value="/updateSystemAuthor",method = RequestMethod.POST)
    Map<String,Object> updateSystemAuthor(@RequestBody SystemAuthor systemAuthor) {

        return systemAuthorService.updateSystemAuthor(systemAuthor);
    }

    @ApiOperation(value = "查询功能详细信息",notes = "查询功能详细信息")
    @RequestMapping(value="/querySystemAuthorById/{id}",method = RequestMethod.POST)
    Map<String,Object> querySystemAuthorById(@PathVariable(value = "id") String id) {

        return systemAuthorService.querySystemAuthorById(id);
    }

    @ApiOperation(value = "删除功能信息",notes = "删除功能详细信息")
    @RequestMapping(value="/deleteSystemAuthorById/{id}",method = RequestMethod.POST)
    Map<String,Object> deleteSystemAuthorById(@PathVariable(value = "id") String id) {

        return systemAuthorService.deleteSystemAuthorById(id);
    }

    @ApiOperation(value = "批量删除功能信息",notes = "批量删除功能详细信息")
    @RequestMapping(value="/deleteSystemAuthor",method = RequestMethod.POST)
    Map<String,Object> deleteSystemAuthor(@RequestBody Map<String, Object> inMap) {
        List<Map<String,Object>> resList = new ArrayList<>();
        Map<String,Object> returnMap = ResultUtil.returnSuccess("成功",null);
        List ids = (List)inMap.get("ids");

        //循环删除
        for (int i=0; i<ids.size(); i++) {
            resList.add(systemAuthorService.deleteSystemAuthorById(ids.get(i)+""));
        }
        //判断结果集
        for (int i = 0; i < resList.size() ; i++) {
            if((resList.get(i).get("status")+"").equals("200")){
                break;
            }else{
                returnMap = ResultUtil.returnError("失败",null);
            }

        }
        return returnMap;

    }
}
